<template>
	<view>

	</view>
</template>

<script>
	const app = getApp();
	import QQMapWX from "@/common/qqmap/qqmap-wx-jssdk.js"
	var qqmapsdk;
	export default {
		props: {

		},
		data() {
			return {

			};
		},
		mounted() {
			qqmapsdk = new QQMapWX({
				key: 'APWBZ-QS7WI-7DLGX-UGZ2E-YI625-A3FBZ' //这里自己的key秘钥进行填充
			});
			if (!app.globalData.city) {
				console.log(1)
				this.getUserLocation()
			} else {
				console.log(2)
				this.$emit('onLoadFun');
			}
		},

		methods: {
			// 微信获得经纬度
			getFuzzyLocation: function() {
				let vm = this;
				wx.getFuzzyLocation({
					type: 'wgs84',
					success: function(res) {
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						app.globalData.latitude = res.latitude;
						app.globalData.longitude = res.longitude;
						console.log('getFuzzyLocation', res);
						vm.getLocal(res.latitude, res.longitude);
					},
					fail: function(res) {
						console.log('getFuzzyLocation-fail', res);
					}
				})
			},
			getUserLocation: function() {
				let vm = this;
				uni.getSetting({
					success: (res) => {
						console.log(JSON.stringify(res))
						// res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
						// res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
						// res.authSetting['scope.userLocation'] == true    表示 地理位置授权
						if (res.authSetting['scope.userLocation'] != undefined && res.authSetting[
								'scope.userLocation'] != true) {
							uni.showModal({
								title: '请求授权当前位置',
								content: '需要获取您的地理位置，请确认授权',
								success: function(res) {
									if (res.cancel) {
										uni.showToast({
											title: '拒绝授权',
											icon: 'none',
											duration: 1000
										})
										vm.$emit('onLoadFun');
									} else if (res.confirm) {
										uni.openSetting({
											success: function(dataAu) {
												if (dataAu.authSetting["scope.userLocation"] ==true) {
													uni.showToast({
														title: '授权成功',
														icon: 'success',
														duration: 1000
													})
													//再次授权，调用wx.getFuzzyLocation的API
													vm.getFuzzyLocation();
												} else {
													uni.showToast({
														title: '授权失败',
														icon: 'none',
														duration: 1000
													})
													vm.$emit('onLoadFun');
												}
											}
										})
									}
								}
							})
						} else if (res.authSetting['scope.userLocation'] == undefined) {
							//调用wx.getFuzzyLocation的API
							console.log(222);
							vm.getFuzzyLocation();
							//vm.$emit('onLoadFun');
						} else {
							console.log(3333);
							//调用wx.getFuzzyLocation的API
							vm.getFuzzyLocation();
							//vm.$emit('onLoadFun');
						}
					}
				})
			},
			// 获取当前地理位置
			getLocal: function(latitude, longitude) {
				let vm = this;
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: latitude,
						longitude: longitude
					},
					success: function(res) {
						let city = res.result.ad_info.city;
						console.log(city)
						app.globalData.city = res.result.ad_info.city;
						console.log('getLocal')
						vm.$emit('onLoadFun');

					},
					fail: function(res) {
						console.log(11111, res)
					},
					complete: function(res) {
						// console.log(res);
					}
				});
			}
		}

	}
</script>

<style lang="scss" scoped>
	.box {
		padding: 50rpx;
		padding-top: 20rpx;
	}

	.title {
		color: #347DFE;
		text-align: center;
		line-height: 90rpx;
		font-size: 32rpx;
		font-weight: 700;
		margin-bottom: 20rpx;
	}

	.btn {
		width: 460rpx;
		height: 90rpx;
		border: 1px solid #347DFE;
		border-radius: 12rpx;
		padding: 0 20rpx;
		color: #666;
		font-size: 28rpx;
	}
</style>
